package com.biz.primus.ms.commodity.dao.redis.saleTag;

import com.biz.primus.base.redis.CrudRedisDao;
import com.biz.primus.base.redis.util.RedisUtil;
import com.biz.primus.commodity.vo.saleTag.SaleTagProductSearchReqVo;
import com.biz.primus.ms.commodity.dao.ro.SaleTagProductRo;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * Created by aaa on 2017/6/29.
 */
@Repository
public class SaleTagProductSortRedisDao extends CrudRedisDao<SaleTagProductRo, String> {
    @Override
    public void save(SaleTagProductRo ro) {
        super.save(ro);
        String saleTagProductAllKey = this.saleTagProductAllMappingKey(ro.getSaleTagId());
        zadd(saleTagProductAllKey, ro.getSort(), ro.getId().getBytes());
        String saleTagProductCategoryKey = this.saleTagProductCategoryMappingKey(ro.getSaleTagId(), ro.getCategoryId());
        zadd(saleTagProductCategoryKey, ro.getSort(), ro.getId().getBytes());
    }

    /**
     * 销售标签和商品分类key
     * @param saleTagId
     * @param categoryId
     * @return
     */
    private String saleTagProductCategoryMappingKey(Long saleTagId, Long categoryId) {
        if (categoryId != null){
            return getKeyByParams(saleTagId, categoryId);
        }else {
            return saleTagProductAllMappingKey(saleTagId);
        }
    }

    /**
     * 销售标签排序Key
     * @param saleTagId
     * @return
     */
    private String saleTagProductAllMappingKey(Long saleTagId) {
        return getKeyByParams(saleTagId, "All");
    }

    /**
     * 销售标签下的商品排序
     * @param reqVo
     * @return
     */
    public List<SaleTagProductRo> findBySaleTag(SaleTagProductSearchReqVo reqVo){
        final String key = this.saleTagProductAllMappingKey(reqVo.getSaleTagId());
        final Set<byte[]> bytes = this.zRange(key, Math.max(0, reqVo.getPageIndex() - 1) * reqVo.getPageSize(),
                Math.max(1, reqVo.getPageIndex()) * reqVo.getPageSize() - 1);
        if(CollectionUtils.isNotEmpty(bytes)){
            return super.findByIds(RedisUtil.bytesSetToStringList(bytes));
        }
        return Lists.newArrayList();
    }

    /**
     * 销售标签和商品分类下的排序
     * @param reqVo
     * @return
     */
    public List<SaleTagProductRo> findBySaleTagAndCategory(SaleTagProductSearchReqVo reqVo){
        final String key = this.saleTagProductCategoryMappingKey(reqVo.getSaleTagId(), reqVo.getCategoryId());
        final Set<byte[]> bytes = this.zRange(key, Math.max(0, reqVo.getPageIndex()) * reqVo.getPageSize(),
                Math.max(1, reqVo.getPageIndex()+1) * reqVo.getPageSize() - 1);
        if(CollectionUtils.isNotEmpty(bytes)){
            return super.findByIds(RedisUtil.bytesSetToStringList(bytes));
        }
        return Lists.newArrayList();
    }

    public int findTotalBySaleTag(SaleTagProductSearchReqVo reqVo) {
        final String key = this.saleTagProductAllMappingKey(reqVo.getSaleTagId());
        final Set<byte[]> bytes = this.zRange(key, 0, -1);
        return bytes.size();
    }
    public int findTotalBySaleTagAndCategory(SaleTagProductSearchReqVo reqVo) {
        final String key = this.saleTagProductCategoryMappingKey(reqVo.getSaleTagId(), reqVo.getCategoryId());
        final Set<byte[]> bytes = this.zRange(key, 0, -1);
        return bytes.size();
    }

    public void delete(List<String> ids){
        final List<SaleTagProductRo> saleTagProductRos = super.findByIds(ids);
        for (SaleTagProductRo saleTagProductRo : saleTagProductRos) {
            super.delete(saleTagProductRo);
            String saleTagProductAllKey = this.saleTagProductAllMappingKey(saleTagProductRo.getSaleTagId());
            String saleTagProductCategoryKey = this.saleTagProductCategoryMappingKey(saleTagProductRo.getSaleTagId(), saleTagProductRo.getCategoryId());
            zrem(saleTagProductAllKey, saleTagProductRo.getId());
            zrem(saleTagProductCategoryKey, saleTagProductRo.getId());
        }
    }
    public void deleteAllBySaleTag(Long saleTagId){
        final String key = this.saleTagProductAllMappingKey(saleTagId);
        final Set<byte[]> bytes = zRange(key, 0, -1);
        if(CollectionUtils.isNotEmpty(bytes)){
            final List<String> ids = RedisUtil.bytesSetToStringList(bytes);
            final List<SaleTagProductRo> saleTagProductRos = super.findByIds(ids);
            for (SaleTagProductRo saleTagProductRo : saleTagProductRos) {
                super.delete(saleTagProductRo);
                String saleTagProductCategoryKey = this.saleTagProductCategoryMappingKey(saleTagProductRo.getSaleTagId(), saleTagProductRo.getCategoryId());
                zrem(saleTagProductCategoryKey, saleTagProductRo.getId());
            }
        }
        zremrangeByRank(key, 0 , -1);
    }
}
